package de.preamp.clients.java.playlist.ui;

import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.IOException;

import javax.swing.JComponent;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.TransferHandler;

import de.preamp.clients.java.data.AbstractDBItem;
import de.preamp.clients.java.data.Track;
import de.preamp.clients.java.playlist.Playlist;

/**
 * @author André Stannek
 * @version 2010-07-30
 */
public class PlaylistTransferHandler extends TransferHandler
{
    /**
     * Generated UID.
     */
    private static final long serialVersionUID = 5372222703451288895L;

    @Override
    protected final Transferable createTransferable(final JComponent component)
    {
	Transferable result = null;
	if (component instanceof JList)
	{
	    final Object object = ((JList) component).getSelectedValue();

	    if (object instanceof Track)
	    {
		result = ((Track) object);
	    }
	}
	return result;
    }

    @Override
    protected final void exportDone(final JComponent source,
	    final Transferable data, final int action)
    {
	final DataFlavor flavor = data.getTransferDataFlavors()[0];
	try
	{
	    final AbstractDBItem transferData =
		    (AbstractDBItem) data.getTransferData(flavor);
	    final Playlist playlist = (Playlist) ((JList) source).getModel();
	    for (final Track track : transferData.getTracks())
	    {
		playlist.remove(track);
	    }
	}
	catch (final UnsupportedFlavorException e)
	{
	    // TODO Auto-generated catch block
	    e.printStackTrace(); // NOPMD
	}
	catch (final IOException e)
	{
	    // TODO Auto-generated catch block
	    e.printStackTrace(); // NOPMD
	}
    }

    @Override
    public final int getSourceActions(final JComponent component)
    {
	return MOVE;
    }

    @Override
    public final boolean canImport(final TransferSupport support)
    {
	boolean result = false;
	// TODO analyse data flavors
	// DataFlavor[] flavors = support.getDataFlavors();
	if (support.getDropAction() == COPY || support.getDropAction() == MOVE)
	{
	    result = true;
	}
	return result;
    }

    @Override
    public final boolean importData(final TransferSupport support)
    {
	if (support.getComponent() instanceof JList)
	{
	    final JList jPlaylist = (JList) support.getComponent();
	    final Playlist playlist = (Playlist) jPlaylist.getModel();
	    int dropIndex =
		    ((JList.DropLocation) support.getDropLocation()).getIndex();

	    if (dropIndex == -1)
	    {
		dropIndex = 0;
	    }

	    final DataFlavor flavor =
		    support.getTransferable().getTransferDataFlavors()[0];
	    try
	    {
		final AbstractDBItem item =
			(AbstractDBItem) support.getTransferable()
				.getTransferData(flavor);
		for (final Track track : item.getTracks())
		{
		    playlist.add(dropIndex++, track);
		}
	    }
	    catch (final IOException e1)
	    {
		JOptionPane.showMessageDialog(null,
			"Connection to server unavailable",
			"server connection error", JOptionPane.ERROR_MESSAGE);
	    }
	    catch (final UnsupportedFlavorException e)
	    {
		JOptionPane.showMessageDialog(null,
			"this can't be dropped here", "",
			JOptionPane.ERROR_MESSAGE);
	    }
	    return true;
	}
	return false;
    }
}
